package com.fjwt.gz.mch.ctrl.activity;

import cn.hutool.json.JSONUtil;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.HelpRecordDTO;
import com.fjwt.gz.db.entity.HelpRecordEntity;
import com.fjwt.gz.db.vo.HelpRecordVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.HelpRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 助力记录
 *
 * @Author: Will
 * @Date: 2023-12-29
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/api/share/record")
public class ShareRecordController extends CommonCtrl {

    @Autowired
    private HelpRecordService helpRecordService;

    /**
     * 新增助力
     * @return
     */
    @PostMapping("/add")
    public ApiRes add() {
        Long fansId = getCurrentUser().getFansId();
        HelpRecordDTO dto = getObject(HelpRecordDTO.class);
        log.info("C端新增助力，请求地址：/api/share/record/add，请求参数:{}", JSONUtil.toJsonStr(dto));
        helpRecordService.savesShareRecord(dto, fansId);
        return ApiRes.ok("助力成功");
    }


    /**
     * 助力记录
     * @return
     */
    @PostMapping(value = "/details")
    public ApiRes details() {
        HelpRecordDTO helpRecordDTO = getObject(HelpRecordDTO.class);
        log.info("C端助力记录，请求地址：/api/share/record/details，请求参数:{}", JSONUtil.toJsonStr(helpRecordDTO));
        List<HelpRecordVO> list = helpRecordService.details(helpRecordDTO);
        List<HelpRecordVO> vos = List.copyOf(list);
        log.info("C端助力记录，请求地址：/api/share/record/details，返回结果:{}", JSONUtil.toJsonStr(list));
        return ApiRes.ok(vos);
    }

    /**
     * 分享记录
     * @return
     */
    @PostMapping(value = "/list")
    public ApiRes list() {
        HelpRecordDTO helpRecordDTO = getObject(HelpRecordDTO.class);
        log.info("C端分享记录，请求地址：/api/share/record/list，请求参数:{}", JSONUtil.toJsonStr(helpRecordDTO));
        helpRecordDTO.setToFansId(getCurrentUser().getFansId());
        List<HelpRecordVO> list = helpRecordService.details(helpRecordDTO);
        log.info("C端分享记录，请求地址：/api/share/record/list，返回结果:{}", JSONUtil.toJsonStr(list));
        List<HelpRecordVO> vos = List.copyOf(list);
        return ApiRes.ok(vos);
    }

    /**
     * 兑换消耗助力次数
     * @return
     */
    @PostMapping("/exchange")
    public ApiRes exchange() {
        HelpRecordDTO helpRecordDTO = getObject(HelpRecordDTO.class);
        log.info("C端兑换消耗助力次数，请求地址：/api/share/record/exchange，请求参数:{}", JSONUtil.toJsonStr(helpRecordDTO));
        Long fansId = getCurrentUser().getFansId();
        String locationAgentNo =request.getHeader("locationAgentNo");
        helpRecordDTO.setAppId(request.getHeader("appid"));
        helpRecordService.exchange(helpRecordDTO, fansId, locationAgentNo);
        return ApiRes.ok("兑换成功");
    }

}

